import { createRouter, createWebHistory } from 'vue-router'
import MainLayout from '../layouts/MainLayout.vue'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import About from '../views/About.vue'
import Profile from '../views/Profile.vue'
import Analysis from '../views/Analysis.vue'
import Wealth from '../views/Wealth.vue'
import Career from '../views/Career.vue'
import Relationship from '../views/Relationship.vue'
import Recharge from '../views/Recharge.vue'
import Payment from '../views/Payment.vue'

const routes = [
  {
    path: '/',
    component: MainLayout,
    children: [
      {
        path: '',
        name: 'Home',
        component: Home
      },
      {
        path: 'about',
        name: 'About',
        component: About
      },
      {
        path: 'profile',
        name: 'Profile',
        component: Profile,
        meta: { requiresAuth: true }
      },
      {
        path: 'analysis',
        name: 'Analysis',
        component: Analysis
      },
      {
        path: 'wealth',
        name: 'Wealth',
        component: Wealth
      },
      {
        path: 'career',
        name: 'Career',
        component: Career
      },
      {
        path: 'relationship',
        name: 'Relationship',
        component: Relationship
      },
      {
        path: 'recharge',
        name: 'Recharge',
        component: Recharge,
        meta: { requiresAuth: true }
      },
      {
        path: 'payment',
        name: 'Payment',
        component: Payment,
        meta: { requiresAuth: true }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem('isLoggedIn')
  
  if (to.meta.requiresAuth && !isLoggedIn) {
    next('/login')
  } else {
    next()
  }
})

export default router 